package DatabaseManaging;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.tools.ant.types.CommandlineJava.SysProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import DatabaseManaging.Models.Product;


public class JdbcProductsDao implements ParametersDAO{
	
	private JdbcTemplate jdbcTemplate;

	public JdbcProductsDao() {
	}

	@Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
    
	public JdbcProductsDao(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////	/////////////////////////////////////////////////////////////////////////////////////////////////
							// Methods// 
	/////////////////////////////////////////////////////////////////////////////////////////////////
   
	public void updateAbbsByName(String id,ArrayList<String> productsAbbs){
    	for(int i=0;i<productsAbbs.size();i++){
    		this.jdbcTemplate.update("UPDATE akn_product_dim SET product_abbr?=? WHERE product_dim_id=?",new Object[] {i+1,productsAbbs.get(i),id});
    	}
    	System.out.println("Succesfully update. Product id:"+id);
    }

	public ArrayList<Object> findElementby(String name) {
		String sql = "SELECT * from akn_product_dim where product_name=?";
		Object[] obj={name};
		ArrayList<Map<String, Object>> resultQuery = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql, obj);
		ArrayList<Object> resultList = fromMapToListElements(resultQuery);

		return resultList;
	}

	public ArrayList<Object> fromMapToListElements(ArrayList<Map<String,Object>> resultQuery) {
		ArrayList<Object> resultList = new ArrayList<Object>();
        for(int i=0;i<resultQuery.size();i++){
    	  Product p=ProductBuilder.build(resultQuery.get(i));
    	  resultList.add(p);
        }
		return resultList;
	}
    
    public void insertElement(Object inputProduct){
    	Product prod=new Product();
    	prod=(Product) inputProduct;
    	String sql="insert into akn_product_dim " +
    	        "(product_name,product_abbr1,product_abbr2,product_abbr3," +
    	        "product_abbr4,product_abbr5,version,version_abbr,category_name,description,sub_category_name) values(?,?,?,?,?,?,?,?,'','','') ";   	
    	Object[] obj = genObjectToInsert(prod);
    	this.jdbcTemplate.update(sql,obj);
    }

	private Object[] genObjectToInsert(Product prod) {
		Object[] obj=new Object[8];
    	obj[0]=prod.getName();
		int j;
		for(j=0;j<prod.getAbbs().size();j++){
		obj[j+1]=prod.getAbbs().get(j);
		}
		while(j<5){
			obj[j+1]="";
			j++;
		}
    	obj[6]=prod.getRelease();
    	obj[7]=prod.getRelease_year();
		return obj;
	}

	@Override
	public Map<ArrayList<String>, String> getMapwithAllElements() {
		String sql = "SELECT * from akn_product_dim";
		ArrayList<Map<String, Object>> resultQuery = (ArrayList<Map<String, Object>>) jdbcTemplate.queryForList(sql);
		HashMap<ArrayList<String>, String>  resultList = toMapElements(resultQuery);

		return resultList;
	}
	
	private HashMap<ArrayList<String>, String> toMapElements(
			ArrayList<Map<String, Object>> resultQuery) {
		HashMap<ArrayList<String>,String> resultMap=new HashMap<ArrayList<String>,String>();
		for(int i=0;i<resultQuery.size();i++){
        	Map<String, Object> mapElements=resultQuery.get(i);
        	Object id=mapElements.get("product_dim_id");
        	ArrayList<String> parametersProducts=new ArrayList<String>();
        	parametersProducts.add((String)mapElements.get("product_name"));
        	for(int j=1;j<6;j++){
        		String abb=(String)mapElements.get("product_abbr"+j);
        		if(!abb.isEmpty()){
        			parametersProducts.add(abb);
        		}
        	}
        	parametersProducts.add((String)mapElements.get("version"));
        	parametersProducts.add((String)mapElements.get("version_abbr"));
        	resultMap.put(parametersProducts,id.toString());
        }
		return resultMap;
	}

	
/////////////////////////////////////////////////////////////////////////////////////////////////	/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
	public static void main(String[] args) throws IOException {
    	 DriverManagerDataSource dataSource = new DriverManagerDataSource();
    	 dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    	 dataSource.setUrl("jdbc:mysql://localhost:3306/ea");
    	 dataSource.setUsername("root");
    	 dataSource.setPassword("");
    	JdbcProductsDao dao=new JdbcProductsDao();
    	dao.setDataSource(dataSource);   
    	
//	    ApplicationContext context = new ClassPathXmlApplicationContext("/DatabaseManaging/Models/application-context-persist.xml");
//	    JdbcProductsDao dao=(JdbcProductsDao) context.getBean("JdbcProductsDao");

//    	JdbcProductsDao dao=new JdbcProductsDao();
//		System.out.println("try it..");
//		ArrayList<String> list=new ArrayList<String>();
//		list.add("NF");
//		dao.updateAbbsByName(Integer.toString(1), list);
		ArrayList<Object> list=dao.findElementby("Autocad");
		System.out.println(dao.getMapwithAllElements());
		System.out.println(list);
//		BufferedWriter fout = new BufferedWriter(new FileWriter(args[0]));
//		for(int i=0;i<list.size();i++){
//			Product p=(Product)list.get(i);
//			fout.write(p.toString());
//			fout.write("\n");
//		}
//		fout.close();
//		Product expected=new Product();
//		expected.setId(1);
//		expected.setName("RIBERY");
//		ArrayList<String> abbs=new ArrayList<String>();
//		abbs.add("ACD");
//		expected.setAbbs(abbs);
//		expected.setRelease("Autocad 2012");
//		expected.setRelease_year("2012");
//		dao.insertElement(expected);
//    	System.out.println("Saccesfully Inserted!");

	//	int mapDao=dao.countOfActorsByFirstName("autocad");
		System.out.println("DONE!");
	}

}